package com.zhuanzhuan.hero.user.helper;

import com.bj58.youpin.common.utils.json.JsonUtils;
import com.zhuanzhuan.hero.biz.common.util.WxWarnTemplateUtil;
import com.zhuanzhuan.hero.user.consts.Consts;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import com.zhuanzhuan.hero.user.pojo.bo.EnterpriseAuthResultBO;
import com.zhuanzhuan.hero.user.pojo.bo.RealnameAuthResultBO;
import com.zhuanzhuan.hero.user.util.Assert;
import com.zhuanzhuan.hunter.log.interceptor.LogContext;
import com.zhuanzhuan.spamlogic.contract.IAuthCommonService;
import com.zhuanzhuan.spamlogic.contract.IBusinessLicenseInfoService;
import com.zhuanzhuan.spamlogic.contract.ICertificationService;
import com.zhuanzhuan.spamlogic.entity.ApiResult;
import com.zhuanzhuan.spamlogic.entity.AuthCommonAllRes;
import com.zhuanzhuan.spamlogic.entity.AuthCommonSensitiveQueryDto;
import com.zhuanzhuan.spamlogic.entity.BusinessLicenseInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Set;

/**
 * @ClassName SpamlogicHeleper
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/27 16:20
 */
@Component
@Slf4j
public class SpamlogicHeleper {

    @Resource
    private IAuthCommonService authCommonService;

    @Resource
    private IBusinessLicenseInfoService businessLicenseInfoService;

    @Resource
    private ICertificationService certificationService;

    /**
     * 查询实人认证结果
     *
     * @param uid
     * @return com.zhuanzhuan.hero.user.pojo.bo.RealnameAuthResultBO
     * @author  hanjunjun
     * @date 2024/6/27
     **/
    public RealnameAuthResultBO queryRealnameAuthResult(Long uid) {
        AuthCommonSensitiveQueryDto dto = new AuthCommonSensitiveQueryDto();
        dto.setUid(uid);
        dto.setAuthTypeNew(Consts.REALNAME_AUTH_TYPE_NEW);
        dto.setSourceCode(Consts.REALNAME_AUTH_SOURCE_CODE);
        dto.setAuthSource(Consts.AUTH_SOURCE_CODE);
        log.info("queryAllSensitiveAuthCommonInfo param={}", JsonUtils.toJsonWithoutNull(dto));
        ApiResult<AuthCommonAllRes> result =
            authCommonService.queryAllSensitiveAuthCommonInfo(dto, Consts.REALNAME_AUTH_KEY, LogContext.getLogStr());
        log.info("queryAllSensitiveAuthCommonInfo result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("queryRealnameAuthResult失败");
            WxWarnTemplateUtil.warnOutService(Consts.SERVICE_NAME, "scf接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }
        AuthCommonAllRes data = result.getData();
        return this.convert(data);
    }

    /**
     * 查询企业认证结果
     *
     * @param uid
     * @return com.zhuanzhuan.hero.user.pojo.bo.EnterpriseAuthResultBO
     * @author  hanjunjun
     * @date 2024/6/27
     **/
    public EnterpriseAuthResultBO queryEnterpriseAuthResult(Long uid) {
        log.info("queryEnterpriseAuthResult uid={}", uid);
        ApiResult<BusinessLicenseInfo> result =
            businessLicenseInfoService.getMultiLicenseInfoByUid(uid, Consts.ENTERPRISE_AUTH_CODE,
                Consts.ENTERPRISE_AUTH_KEY, LogContext.getLogStr());
        log.info("queryEnterpriseAuthResult result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("queryEnterpriseAuthResult失败");
            WxWarnTemplateUtil.warnOutService(Consts.SERVICE_NAME, "scf接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }
        BusinessLicenseInfo data = result.getData();
        return this.convertEnterpriseAuthResult(data);
    }

    /**
     * 根据uid查询同一认证信息下所有的uid  包含入参uid
     *
     * @param uid
     * @return java.util.Set<java.lang.Long>  未实人则返回空
     * @author  hanjunjun
     * @date 2024/6/28
     **/
    public Set<Long> getSameIdCardAllUid(Long uid) {
        Assert.notNull(uid, ExceptionMsgEnum.BUSINESS_ERROR);
        log.info("getSameIdCardUid uid={}", uid);
        try {
            Set<Long> allUid =
                certificationService.getSameIdCardUid(uid, Consts.AUTH_SOURCE_CODE, LogContext.getLogStr());
            log.info("getSameIdCardUid result={}", allUid);
            return allUid;
        } catch (Exception e) {
            log.error("getSameIdCardUid异常", e);
            WxWarnTemplateUtil.warnOutService(Consts.SERVICE_NAME, "scf接口调用失败");
            throw new BusinessException("getSameIdCardUid请求异常", e);
        }
    }



    private EnterpriseAuthResultBO convertEnterpriseAuthResult(BusinessLicenseInfo data) {
        //未认证则中台返回null
        if (Objects.isNull(data)) {
            return null;
        }
        EnterpriseAuthResultBO enterpriseAuthResultBO = new EnterpriseAuthResultBO();
        enterpriseAuthResultBO.setBusinessName(data.getBusinessName());
        enterpriseAuthResultBO.setFrName(data.getFrName());
        return enterpriseAuthResultBO;
    }

    private RealnameAuthResultBO convert(AuthCommonAllRes data) {
        RealnameAuthResultBO realnameAuthResultBO = new RealnameAuthResultBO();
        realnameAuthResultBO.setUid(data.getUid());
        realnameAuthResultBO.setSelfAuthStatus(data.getSelfAuthStatus());
        realnameAuthResultBO.setSelfAuthName(data.getSelfAuthName());
        realnameAuthResultBO.setSelfAuthIdCard(data.getSelfAuthIdCard());
        return realnameAuthResultBO;
    }

}
